package com.yanqu.road.server.cmd.travel;

import com.yanqu.road.entity.enums.eTravelEventType;
import com.yanqu.road.entity.travel.TravelEvent;
import com.yanqu.road.entity.travel.TravelSelectObjectResult;
import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.pb.travel.TravelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.travel.TravelModule;
import com.yanqu.road.server.manger.config.TravelMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_TRAVEL_SELECT_OBJECT, desc = "选择事件对象")
public class TravelSelectObjectCmd extends AbstractCommand {

	private int objectId;

	// CMD处理前的赚速
	private String preEarnSpeed;

	private TravelSelectObjectResult selectObjectResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TravelProto.TravelSelectObjectReqMsg reqMsg = TravelProto.TravelSelectObjectReqMsg.parseFrom(packet.getBytes());
		objectId = reqMsg.getObjectId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		preEarnSpeed = player.getUserInfo().getEarnSpeed().toString();

		selectObjectResult = new TravelSelectObjectResult();
		TravelModule travelModule = player.getModule(TravelModule.class);
		UserTravel userTravel = travelModule.getUserTravel();
		if(null == userTravel){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_IS_LOCK);
			return;
		}
		int eventId = userTravel.getEventId();
		if(0 == eventId){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_NO_SPECIAL_EVENT);
			return;
		}
		TravelEvent travelEvent = TravelMgr.getTravelEvent(eventId);
		if(null == travelEvent){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_EVENT_ERROR);
			log.info("travel select object,event error,userId:{},eventId:{},select objectId:{}", player.getUserId(), eventId, objectId);
			userTravel.removeEventId();
			return;
		}
		if(eTravelEventType.AppointTrystEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectTrystBeauty(travelEvent, objectId,0);
			travelModule.syncUserTravel();
		}else if(eTravelEventType.BrothelEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectBrothelBeauty(travelEvent, objectId);
			if(0 != selectObjectResult.getRet()){
				travelModule.syncUserTravel();
			}
		}else if(eTravelEventType.PatronsWineEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectWinePatrons(travelEvent, objectId,0);
			travelModule.syncUserTravel();
		}else if(eTravelEventType.CasinoEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectCasinoType(travelEvent, objectId);
		}else if(eTravelEventType.DrinkEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectIsDrink(travelEvent, objectId);
			travelModule.syncUserTravel();
		}else {
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_EVENT_ERROR);
			log.info("travel select object,event error,userId:{},eventId:{},select objectId:{}", player.getUserId(), eventId, objectId);
			userTravel.removeEventId();
			travelModule.syncUserTravel();
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		TravelProto.TravelSelectObjectRespMsg.Builder respMsg = TravelProto.TravelSelectObjectRespMsg.newBuilder();
		respMsg.setRet(selectObjectResult.getRet());
		respMsg.setEventId(selectObjectResult.getEventId());
		respMsg.setObjectId(selectObjectResult.getObjectId());
		respMsg.setAddValue(selectObjectResult.getAddValue());

		respMsg.setPreEarnSpeed(preEarnSpeed);
		respMsg.setNextEarnSpeed(player.getUserInfo().getEarnSpeed().toString());

		player.sendPacket(Protocol.U_TRAVEL_SELECT_OBJECT, respMsg);
	}
}
